package com.live.task;

import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.live.activity.LiveMessageActivity;
import com.live.activity.LiveUserLoginActivity;
import com.live.activity.R;
import com.live.common.Constant;
import com.live.tools.LiveTools;

public class LiveFriendCheck extends AsyncTask<Bundle, Integer, String> {

	private Context context;

	public Button friendBtn;

	public String uId;

	public String fId;

	public LiveFriendCheck(Context context, Button friendBtn) {
		this.context = context;
		this.friendBtn = friendBtn;
	}

	// 此方法在主线程执行,当任务执行之前开始调用此方法，可以在这里显示进度对话框
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		Log.i("PageTask", "onPreExecute()  " + Thread.currentThread().getId());
	}

	// 此方法在后台线程执行，完成任务的主要工作，通常需要较长的时间。
	// 在执行过程中可以调用publicProgress(Progress...)来更新任务的进度。

	@Override
	protected String doInBackground(Bundle... params) {
		Log.i("PageTask", "doInBackground... " + Thread.currentThread().getId());

		uId = params[0].getString("uId");

		fId = params[0].getString("fId");

		String url = Constant.SITE_URL + "live_friend_check.php?locale=" + LiveTools.getLocaleLanguage() + "&uId="
				+ uId + "&fId=" + fId;

		return getFriendState(url);
	}

	// 此方法在主线程执行，用于显示任务执行的进度
	@Override
	protected void onProgressUpdate(Integer... values) {
		super.onProgressUpdate(values);
		Log.i("PageTask", "onProgressUpdate()  " + Thread.currentThread().getId());
	}

	// 此方法在主线程执行，任务执行的结果作为此方法的参数返回。
	@Override
	protected void onPostExecute(String result) {
		Log.i("PageTask", "onPostExecute()  " + Thread.currentThread().getId());

		if (Constant.ACTION_ALLOW.equals(result)) {
			// 设置发送消息按钮
			friendBtn.setText(R.string.message_btn);
			friendBtn.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setClass(context, LiveMessageActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString("uId", uId);
					bundle.putString("fId", fId);
					intent.putExtras(bundle);
					context.startActivity(intent);
				}
			});
		} else if (Constant.ACTION_APPLY.equals(result)) {
			friendBtn.setText(R.string.check_btn);
			friendBtn.setBackgroundResource(R.drawable.gray_func_btn);
			friendBtn.setPadding(3, 0, 0, 3);
		} else {
			// 设置关注好友按钮
			friendBtn.setText(R.string.apply_btn);
			friendBtn.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					if (uId != null) {
						// 用户已登录
						String url = Constant.SITE_URL + "live_friend.php?locale=" + LiveTools.getLocaleLanguage()
								+ "&";
						url += "fUId=" + fId + "&uId=" + uId + "&fState=" + Constant.ACTION_APPLY;
						Bundle bundle = new Bundle();
						bundle.putString("action", Constant.ACTION_APPLY);
						LiveFriend task = new LiveFriend(context, url, null, null, friendBtn, null, null, null);
						task.execute(bundle);
					} else {
						// 用户未登录
						Intent intent = new Intent();
						intent.setClass(context, LiveUserLoginActivity.class);
						context.startActivity(intent);
					}
				}
			});
		}
		friendBtn.setVisibility(Constant.VISIBLE);
	}

	/**
	 * 请求/通过好友
	 * 
	 * @param url
	 * @return
	 */
	public String getFriendState(String url) {
		String result = context.getString(R.string.net_error);
		HttpClient client = new DefaultHttpClient();
		HttpPost request;
		try {
			// 提拱默认的HttpClient实现
			request = new HttpPost(new URI(url));
			HttpResponse response = client.execute(request);
			// 判断请求是否成功
			if (response.getStatusLine().getStatusCode() == 200) { // 200表示请求成功
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					String out = EntityUtils.toString(entity);
					result = new JSONObject(out).getString("result");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}